require('../../assets/js/property.js')
require('../../assets/css/index.less')
require('./reg.less')
//引入请求工具
const request = require('../../intils/reques')
//1定义变量引入验证码插件
const CaptchaMini = require('captcha-mini')
//dom加载完毕
window.onload = () => {
    /*不传值，统一配置默认值
     */
    //创建验证码实例对象，配置字体大小
    let captcha1 = new CaptchaMini({
        fontSize: 60,
        dotNum: 100,
        length: 4,
    })
    //3
    let codeImg
    captcha1.draw(document.querySelector('.codeImg'), r => {
        //打印r
        codeImg = r
       // console.log(r, '验证码1');

    })
    $('#submitBtn').addEventListener('click', () => {
        let errMsg = ''
        if (!/^1[3-9]\d{9}$/.test($('#phoneIpt').value)) {
            errMsg = '请输入正确手机号码'
            //console.log('errMsg :>> ', errMsg);
        } else if ($('#codeIpt').value.toLowerCase() !== codeImg.toLowerCase()) {
            errMsg = '请输入正确验证码'
            //console.log('errMsg :>> ', errMsg);
        } else if (!/^\w{3,6}$/.test($('#psdIpt').value)) {
            errMsg = '请输入3-6位密码'
            //console.log('errMsg :>> ', errMsg);
        } else if (!/^\w{3,6}$/.test($('#psdTwinsIpt').value)) {
            errMsg = '再次输入密码'
            //console.log('errMsg :>> ', errMsg);
        } else {
            //验证成功发送请求
            //console.log('验证成功发送请求 :>> ', '验证成功发送请求');
            request.post('/api/user/isExist', {
                    account: $('#phoneIpt').value
                })
                .then(res => {
                    console.log('res :>> ', res);
                    //解构数据
                    let {
                        errno,
                        message
                    } = res.data
                    //判断
                    if (errno === 0) {
                        //发送请求 
                        request.post('/api/user/register',{
                            account: $('#phoneIpt').value,
                            password:$('#psdIpt').value

                        }).then(res=>{
                            let {errno,message}=res.data
                            if(errno===0){
                                alert('注册成功')
                                location.href='./login.html'
                            } else{
                                alert(message)
                            }
                        })

                    } else {
                        //弹出错误信息
                        alert(message)
                    }
                })
               
        }
    })
}